package com.studentsys.dao;

import com.studentsys.bean.Student;
import com.studentsys.utils.BaseDao;
import com.studentsys.utils.PageUtils;

import java.util.List;

public class StudentDao extends BaseDao<Student> {
    public int insert(Student stu){
        String sql = "INSERT INTO student(sname,age,sex,pic,degree,email,mark)\n" +
                "VALUES(?,?,?,?,?,?,?);";
        return adu(sql,stu.getSname(),stu.getAge(),stu.getSex(),stu.getPic(),stu.getDegree(),stu.getEmail(),stu.getMark());
    }
    public List<Student> selectAll(){
        String sql = "select * from student";
        return queryAll(sql,Student.class);
    }
    //分页查询
    public List<Student> findByPage(PageUtils page){
        String sql = "select * from student limit ?,?";
        return queryAll(sql,Student.class,page.getStartIndex(),page.getPageSize());
    }
    //查询学生表中总条数
    public int getTotalCount(){
        String sql = "select count(*) from student";
        Object obj = queryScalar(sql);
        int count = Integer.parseInt(obj.toString());
        return count;
    }
    public Student selectOne(int sid){
        String sql = "select * from student where sid = ?";
        return queryOne(sql,Student.class,sid);
    }
    public int  updateStudent(Student stu){
        String sql = "update student set sname=?,age=?,sex=?,pic=?,degree=?,email=?,mark=? where sid=?";
        return adu(sql,stu.getSname(),stu.getAge(),stu.getSex(),stu.getPic(),stu.getDegree(),stu.getEmail(),stu.getMark(),stu.getSid());
    }
    //根据学号删除学生
    public int deleteStudent(int sid){
        String sql = "delete from student where sid =?";
        return adu(sql,sid);
    }

    //邮箱唯一性验证的查询
    public Student checkEmail(String email) {
        String sql = "select * from student where email = ?";
        return queryOne(sql,Student.class,email);
    }


    public Student checkSname(String sname) {
        String sql = "select * from student where sname = ?";
        return queryOne(sql,Student.class,sname);
    }

    public Student checkAge(String age) {
        String sql = "select * from student where age = ?";
        return queryOne(sql,Student.class,age);
    }
}
